package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.adapter.ProductAdapter;
import com.zhuannimei.dialog.BaseDialog;
import com.zhuannimei.domain.Product;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.ProductParser;
import com.zhuannimei.service.AsyncDataLoader;
import com.zhuannimei.service.AsyncDataLoader.Callback;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.YouLog;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.utility.ZnmHttpParams;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

/**
 * 积分商城
 * 
 * @author Jack 2014-1-9
 * 
 * 
 */
public class TabTwoActivity extends BaseActivity implements
		OnItemClickListener, OnRefreshListener, OnCancelListener {

	private static final String TAG = "TabTwoActivity";
	private ZnmRefreshListView znmRL;// 赚你妹下拉控件
	private List<Product> mProducts = new ArrayList<Product>();
	private ProductAdapter mAdapter;
	private int mPage = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fragment2);
		initView();
		setListener();
		setData();
	}

	

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("积分商城");
	}

	/**
	 * 从服务器上获取商品列表
	 */
	private void getProductsFromNet() {

		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("pageIndex", mPage + "");
		RequestServerData.productList(mParams, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					JSONObject jo = new JSONObject(responseInfo.result);
					if ("0".equals(jo.optString("code"))) {
						List<Product> list = new ProductParser()
								.parseJSON(responseInfo.result);
						if (list != null && list.size() != 0) {
							mProducts.clear();
							mProducts.addAll(list);
							WuLog.i(TAG, "list==");
						}
						znmRL.setAdapter(mAdapter);
						mAdapter.notifyDataSetChanged();
						// 先删后存
						GlobalApplication.appDB.deleteByWhere(Product.class,
								null);
						for (Product pu : mProducts) {
							GlobalApplication.appDB.save(pu);
						}
						znmRL.onRefreshComplete();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(HttpException error, String msg) {
				YouLog.i(TAG, "错误：" + error + "----" + "错误信息：" + msg);
				showCustomToast("网络不给力");
			}
		});
	}

	private void setData() {
		// 先查本地数据
		new AsyncDataLoader(new Callback() {

			@Override
			public void onStart() {
				try {
					mProducts.clear();
					List<Product> list = GlobalApplication.appDB
							.findAll(Product.class);
					if (list != null && list.size() != 0) {
						mProducts.addAll(list);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onPrepare() {
				mAdapter = new ProductAdapter(TabTwoActivity.this, mProducts);
				znmRL.setAdapter(mAdapter);
				znmRL.onManualRefresh();
			}

			@Override
			public void onFinish() {
				getProductsFromNet();
			}
		}).execute();
	}

	private void setListener() {
		znmRL.setOnItemClickListener(this);
		znmRL.setOnRefreshListener(this);
		znmRL.setOnCancelListener(this);
	}

	private void initView() {
		znmRL = (ZnmRefreshListView) findViewById(R.id.point_shop_ZR);
	}

	@Override
	public void onCancel() {
		znmRL.onRefreshComplete();
	}

	@Override
	public void onRefresh() {
		WuLog.i(TAG, "userId==" + ZnmData.getUserData().getUserUserId());
		getProductsFromNet();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	}

}
